package app

import (
	"encoding/json"

	"github.com/douyu/jupiter/pkg/conf"
	"github.com/douyu/jupiter/pkg/server/xgoframe"
	"github.com/douyu/jupiter/pkg/server/xgrpc"
	"github.com/douyu/jupiter/pkg/store/gorm"
	"github.com/douyu/jupiter/pkg/util/xtime"
)

//服务配置
type ServerConfig struct {
	Http struct {
		Port int `json:"port"`
	} `json:"http"`
	Grpc struct {
		Port int `json:"port"`
	} `json:"grpc"`
	Mysql struct {
		Dns string `json:"dns"`
	} `json:"mysql"`
	Redis struct {
		Address  string `json:"address"`
		Password string `json:"password"`
	} `json:"redis"`
	Other map[string]map[string]interface{}
}

func newServerConfig(s string) (ServerConfig, error) {
	data := ServerConfig{}
	err := json.Unmarshal([]byte(s), &data)
	return data, err
}

//func (c NaConfig) Get() interface{} {
//	if c.Param != nil {
//		return nil
//	}
//	return nil
//}

/*




 */

//设置配置
func (c *ServerConfig) Set() {
	conf.Set("jupiter.server.http", xgoframe.Config{
		Host:  "127.0.0.1",
		Port:  c.Http.Port,
		Debug: false,
	})
	conf.Set("jupiter.mysql.test", gorm.Config{
		DSN:             c.Mysql.Dns,
		Debug:           false,
		MaxIdleConns:    10,
		MaxOpenConns:    100,
		ConnMaxLifetime: xtime.Duration("300s"),
		OnDialError:     "panic",
		SlowThreshold:   xtime.Duration("500ms"),
		DialTimeout:     xtime.Duration("1s"),
		DisableMetric:   false,
		DisableTrace:    false,
	})
	conf.Set("jupiter.server.grpc", xgrpc.Config{
		Host:                      "127.0.0.1",
		Port:                      c.Grpc.Port,
		Network:                   "tcp4",
		DisableTrace:              false,
		DisableMetric:             false,
		SlowQueryThresholdInMilli: 0,
		ServiceAddress:            "",
	})
}

/*











 */

type Cfg struct {
	HttpPort  int
	GrpcPort  int
	SqlDns    string
	RedisAddr string
	RedisPwd  string
}
